import { Promise } from 'drunk-base';
import { getJsonWithToken, postJsonWithToken, putJsonWithToken, deleteJsonWithToken } from '../../../service/HttpClient';
import { HOST } from '../../../service/Config';
import { IAdsModel } from './AdsModel';
import { IPageMetaData } from '../../ListModel';

export function getAds(id: string): Promise<IAdsModel> {
    return getJsonWithToken(`${HOST}/content/standard/ads/${id}`);
}

export function getAdsList(page: number = 0, size: number = 20): Promise<{ data: IAdsModel[]; meta: IPageMetaData; }> {
    return getJsonWithToken(`${HOST}/content/standard/ads?page=${page}&size=${size}`);
}

export function createAds(data: IAdsModel): Promise<IAdsModel> {
    return postJsonWithToken(`${HOST}/content/standard/ads`, data);
}

export function updateAds(id: string, data: IAdsModel) {
    return putJsonWithToken(`${HOST}/content/standard/ads/${id}`, data);
}

export function deleteAds(id: string) {
    return deleteJsonWithToken(`${HOST}/content/standard/ads/${id}`);
}

export function getAdsOptionsByName(name: string) {
    return getJsonWithToken(`${HOST}/content/standard/ads?name=${name}&page=0&size=100`)
        .then((res: { data: IAdsModel[], meta: IPageMetaData }) =>
            res.data.map(item => ({ id: item.id, name: item.name }))
        );
}

export function search(name: string, startTime: number, endTime: number, page = 0, size = 20): Promise<{ data: IAdsModel[], meta: IPageMetaData }> {
    return getJsonWithToken(`${HOST}/content/standard/ads?name=${name}&startTime=${startTime}&endTime=${endTime}&page=${page}&size=${size}`);
}
